package org.mycub.server.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.mycub.server.JettyServer;
import org.mycub.server.dataprovider.FreeParkingsProvider;
import org.mycub.server.dataprovider.factory.FreeParkingsProviderFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Path("/freeparking")
public class FreeParkingsService {
	
	@Path("/hello")
	@GET
	public Response getHello() {

		String output = "Hello MyCUB World";
		return Response.status(200).entity(output).build();

	}
	
	@Path("/{lon}/{lat}/{ray}")
	@GET
	public Response getParkingsJSON(
			@PathParam("lon") double longitude, 
			@PathParam("lat") double latitude,
			@PathParam("ray") double rayon) {
	

		long start = JettyServer.getTime();
		System.out.println("\nService requested : " + this.getClass().getSimpleName());
		
		FreeParkingsProvider freeParkings = new FreeParkingsProviderFactory().createFreeParkingsProvider();
		
		rayon = (180*rayon)/((double)Math.PI*6371000);
		freeParkings.setParkings(freeParkings.getParkings(longitude, latitude, rayon));
	   
		int cpt = freeParkings.getParkings().size();
		
		Gson gson = new GsonBuilder().setPrettyPrinting().create();		
		Response response = Response.status(200).entity(gson.toJson(freeParkings)).build();
		
		long stop = JettyServer.getTime();
		System.out.println("Request served in : " + (stop - start) + " ms");
		System.out.println("Data retrieved : " + cpt);
		
		return response;

	}
}
